package com.greate.community.controller;

import com.greate.community.entity.Result;
import com.greate.community.service.VisitDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 网站数据
 */
@RestController
public class DataController {

    @Autowired
    private VisitDataService dataService;

    /**
     * 统计网站 uv
     * @return
     *
     *
     */
    @PostMapping("/data/uv")
    @ResponseBody
    public Result getUV(@RequestBody Map<String,String> map) throws ParseException {
        String start = map.get("start");
        String end = map.get("end");

        HashMap<String,Object> hashMap = new HashMap<>();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        long uv = dataService.calculateUV(format.parse(start), format.parse(end));
        hashMap.put("uvResult", uv);

        hashMap.put("uvStartDate", start);

        hashMap.put("uvEndDate", end);

        return new Result("success", Result.SUCCESS, hashMap);
    }

    /**
     * 统计网站 DAU
     * @return
     */
    @PostMapping("/data/dau")
    @ResponseBody
    public Result getDAU(@RequestBody Map<String,String> map) throws ParseException {
        String start = map.get("start");
        String end = map.get("end");

        HashMap<String,Object> hashMap = new HashMap<>();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        long dau = dataService.calculateUV(format.parse(start), format.parse(end));
        hashMap.put("dauResult", dau);

        hashMap.put("dauStartDate", start);

        hashMap.put("dauEndDate", end);

        return new Result("success", Result.SUCCESS, hashMap);
    }

}
